<template>
	<view class="cu-bar text-black bg-white" style="width: 100vw;height: 10vh;">
		<view class="action">
			<text class="cuIcon-back" @click="returnBcak()"></text>
		</view>
		<view class="content">
			发表动态
		</view>
	</view>

	<view class="bg-white" style="width: 100vw;height: 90vh;">
		<view class="cu-form-group" style="margin-bottom: 5vh;">
			<textarea maxlength="-1" placeholder="请输入动态" v-model="content"></textarea>
		</view>
		<form>
			<view class="cu-form-group">
				<view class="grid col-3 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage"
						:data-url="imgList[index]">
						<image :src="imgList[index]" mode="aspectFill" style="border-radius: 15px;"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids bg-blue light" style="border-radius: 15px;" @tap="ChooseImage"
						v-if="imgList.length<9">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">是否为打卡</view>
				<radio style="border: 1px solid black;border-radius: 75%;" :class="radio==='true'?'checked':''"
					:checked="radio===true" value="true" @click="handleRadioClick"></radio>
			</view>

			<view class="cu-form-group" v-if="radio==true">
				<view class="title">队伍:</view>
				<picker @change="PickerChange" :value="index" :range="picker" style="z-index: 9999;">
					<view class="picker" style="text-align: center;">
						{{ index > -1 ? picker[index] : '请选择队伍' }}
					</view>
				</picker>
			</view>

			<view class="cu-form-group margin-top">
				<view class="title">是否为发布匹配</view>
				<radio style="border: 1px solid black;border-radius: 75%;" :class="radio2==='true'?'checked':''"
					:checked="radio2===true" value="true" @click="handleRadioClickPub"></radio>
			</view>
		</form>
		<button class="cu-btn block bg-blue lg" style="width: 100vw;margin-top: 5vh;" @click="public">
			<text class="cuIcon-upload"></text>发表动态
		</button>
	</view>
</template>

<script>
	import {
		postLive,
		uploadimg,
		publicMatch,
		MyTeam
	} from '@/store/api.js'
	export default {
		onLoad(value) {
			this.groupId = value.groupId
			MyTeam(1, 20).then(res => {
				res.data.data.rows.forEach(item => {
					this.picker.push(item.name)
					this.picker_id.push({
						id: item.id,
						name: item.name
					})
				})
			})
		},
		data() {
			return {
				content: '',
				groupId: '',
				index: -1,
				imgList: [],
				radio: false,
				radio2: false,
				// picker: ['Meet', 'Aeet', 'Beet'],
				picker: [],
				picker_id: [],
				index: -1,
			};
		},
		methods: {
			public() {
				if (this.radio2) {
					let res_content = ''
					let teamId = ''
					if (this.imgList.length > 0) {
						res_content = this.content + '/*img*/' + JSON.stringify(this.imgList)
					} else {
						res_content = this.content
					}
					let data = {
						groupId: this.groupId,
						title: '动态',
						content: res_content,
						meetAddress: '社区动态',
						beginTime: this.getCurrentTimeFormatted,
						endTime: this.getCurrentTimeFormatted,
						latitude: '',
						longtitude: '',
						tags: [{
							id: 12,
							name: '其他'
						}]
					}
					publicMatch(data).then(res => {
						console.log(res)
						uni.emit('restore',true)
						uni.showToast({
							icon: 'success',
							title: '发布成功'
						})
						setTimeout(()=>{
							uni.navigateBack()
						},1000)
						
					}).catch(err => {
						console.log(err)
						uni.showToast({
							icon: 'err',
							title: '发布失败'
						})
					})
				} else {
					let res_content = ''
					let teamId = ''
					if (this.imgList.length > 0) {
						res_content = this.content + '/*img*/' + JSON.stringify(this.imgList)
					} else {
						res_content = this.content
					}
					if (this.radio && this.index != -1) {
						teamId = this.picker_id[this.index].id
					}
					let data = {
						title: '标题',
						content: res_content,
						groupId: this.groupId,
						isSignin: this.radio ? 1 : 0,
						teamId: teamId
					}
					postLive(data).then(res => {
						console.log(res)
						uni.$emit('restore',true)
						uni.showToast({
							icon: 'success',
							title: '发布成功'
						})
						setTimeout(()=>{
							uni.navigateBack()
						},1000)
					}).catch(err => {
						uni.showToast({
							icon: 'err',
							title: '发布失败'
						})
					})
				}
			},
			getCurrentTimeFormatted() {
				const now = new Date();

				const pad = (number) => (number < 10 ? '0' + number : number);

				const year = now.getFullYear();
				const month = pad(now.getMonth() + 1);
				const day = pad(now.getDate());

				const hours = pad(now.getHours());
				const minutes = pad(now.getMinutes());
				const seconds = pad(now.getSeconds());
				const milliseconds = String(now.getMilliseconds()).padStart(3, '0');

				const offset = -now.getTimezoneOffset();
				const offsetSign = offset >= 0 ? '+' : '-';
				const offsetHours = pad(Math.floor(Math.abs(offset) / 60));
				const offsetMinutes = pad(Math.abs(offset) % 60);

				return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${milliseconds}${offsetSign}${offsetHours}:${offsetMinutes}`;
			},
			PickerChange(e) {
				this.index = e.detail.value;
			},
			handleRadioClick() {
				this.radio = !this.radio
				if (this.radio == true) {
					this.radio2 = false
				}
			},
			handleRadioClickPub() {
				this.radio2 = !this.radio2
				if (this.radio2 == true) {
					this.radio = false
				}
			},
			ChooseImage() {
				console.log('看这里')
				uni.chooseImage({
					count: 9, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						console.log(res.tempFilePaths)
						res.tempFilePaths.forEach(item => {
							console.log(item)
							uploadimg(item).then(res => {
								console.log(res)
								this.imgList.push(JSON.parse(res).data)
							})
						})
					}
				});
			},
			returnBcak() {
				uni.navigateBack({
					delta: 1
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				this.imgList.splice(e.currentTarget.dataset.index, 1)
			}
		}
	}
</script>
<style>

</style>